<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="projectName">
            <!-- <el-input v-model="form.projectName" readonly placeholder="自动生成" /> -->
            <el-input v-model="form.projectName" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showTargetSetListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="完工程度" prop="finishPercent">
            <el-input v-model="form.finishPercent" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目负责人" prop="projectHead">
            <el-input v-model="form.projectHead" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目会计" prop="projectAccount">
            <el-input v-model="form.projectAccount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目经营情况" prop="projectDesc">
            <el-input v-model="form.projectDesc" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- <div style="padding-bottom: 10px;">
        <el-button type="primary" @click="showTargetSetListModal = true">引入计划</el-button>
        <el-button type="primary" @click="deleteDetail">删除计划</el-button>
      </div> -->
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
        row-key="number"
        :tree-props="{children: 'childrens', hasChildren: 'hasChildren'}"
      >
        <!-- <template v-slot:contractWorkVolume="{row}">
          <el-input v-model="row.contractWorkVolume" />
        </template>
        <template v-slot:actualCompletion="{row}">
          <el-input v-model="row.actualCompletion" />
        </template> -->
        <template v-slot:amount="{row}">
          <el-input v-model="row.amount" :disabled="disableForm" />
        </template>

      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

    </el-form>
  </div>
</template>

<script>
import { addCostSettleInfo, updateCostSettleInfo, getCostSettleInfoDetail } from '@/api/completedManage'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import moment from 'moment'
export default {
  name: 'AddProjectTotalCostPlanInfo',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        projectAccount: '',
        projectHead: '',
        projectDesc: '',
        finishPercent: ''
      },
      columns: [
        // {
        //   label: '序号',
        //   prop: 'sequence',
        //   width: '60'
        // },
        {
          label: '标题',
          prop: 'title'
        },
        {
          label: '数据',
          slot: 'amount'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        projectName: [
          { required: true, message: '项目名称', trigger: 'change' }
        ]
        // unifiedSocialCreditCode: [
        //   { required: true, message: '统一社会信用代码', trigger: 'change' }
        // ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.businessId) {
          this.getDocDetail(newData.businessId)
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    initTableData() {
      this.targetList = [
        {
          sequence: '1',
          title: '一、目标成本',
          amount: ''
        },
        {
          sequence: '2',
          title: '二、工程账面成本',
          amount: ''
        },
        {
          sequence: '3',
          title: '三、项目盈利',
          amount: ''
        },
        {
          sequence: '4',
          title: '四、项目利润率：',
          amount: ''
        },
        {
          sequence: '5',
          title: '五、其他情况说明：',
          amount: ''
        }
      ]
    },
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    getSelectedRows(obj) {
      // 将值赋给
      this.form = {
        projectName: obj.row.projName
      }
      // const formateData = (data) => {
      //   return data.map(item => {
      //     const { number, name, unit, childrens, parentId } = item
      //     return {
      //       parentId,
      //       number,
      //       name,
      //       unit,
      //       ...childrens ? {
      //         childrens: formateData(childrens)
      //       } : {},
      //       'constSumPrice': '',
      //       'contractCount': '',
      //       'contractSumPrice': '',
      //       'contractUnitPrice': '',
      //       'costUnitPrice': '',
      //       handedAmount: '',
      //       description: ''
      //     }
      //   })
      // }
      // this.targetList = formateData(obj.list)
    },
    getDocDetail(id) {
      getCostSettleInfoDetail({ costSettleInfoId: id }).then(res => {
        const { lstAttachment, costSettleInfo, lstCostSettleDetail } = res.data || {}
        Object.keys(this.form).forEach((key) => {
          this.form[key] = costSettleInfo[key]
        })
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstCostSettleDetail && lstCostSettleDetail.length > 0) {
          this.targetList = lstCostSettleDetail
        }
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            costSettleInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstCostSettleDetail: this.targetList.map(item => {
              return {
                ...item,
                ...this.optType === 'edit' ? {
                  costSettleInfoId: this.data.id
                } : {}
              }
            })
          }
          const fn = this.optType === 'add' ? addCostSettleInfo : updateCostSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
